package dao;

import common.Homework;
import service.FlushCourse;
import util.SnowflakeIdGenerator;
import util.StreamUtils;

import java.io.File;
import java.sql.*;

public class SubmitHW {
    private static Connection conn=DataBaseConnect.getConn();
    private static String sql="INSERT INTO homework (assignmentid,student_name,studentid,student_number) VALUES(?,?,?,?)";
    private static String sql2="INSERT INTO answer (homeworkid,content,file_name,file_path,pic_name,pic_path,question) VALUES(?,?,?,?,?,?,?)";
    private static PreparedStatement pstmt;
    private static PreparedStatement pstmt2;
    static {
        try {
            pstmt=conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstmt2=conn.prepareStatement(sql2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void submitHW(Homework homework){
        try {
            pstmt.setString(1,homework.getAssignmentid());
            pstmt.setString(2,homework.getStudentname());
            pstmt.setString(3,homework.getStudentid());
            pstmt.setString(4,homework.getStudentnumber());
            int count=pstmt.executeUpdate();
            if(count==1){
                ResultSet rs=pstmt.getGeneratedKeys();
                while (rs.next()){
                    pstmt2.setString(1, rs.getString(1));
                    for(int i=0;i<homework.getAnswers().size();i++){
                        pstmt2.setString(2,homework.getAnswers().get(i).getContent());
                        String path="";
                        String name="";
                        if(homework.getAnswers().get(i).getFile()!=null){
                            name=homework.getAnswers().get(i).getFile().getFilename();
                            path="D:\\javacode\\SuperStarDemo\\SuperStarServer\\src\\file\\"+SnowflakeIdGenerator.ID(12)+"."+name.split("\\.")[1];
                            StreamUtils.byteArrayToStream(homework.getAnswers().get(i).getFile().getContent(),path);
                        }
                        //将文件目录入库
                        pstmt2.setString(3,name);
                        pstmt2.setString(4,path);
                        path="";
                        name="";
                        //将图片目录入库
                        if(homework.getAnswers().get(i).getPic()!=null){
                            name=homework.getAnswers().get(i).getPic().getFilename();
                            path="D:\\javacode\\SuperStarDemo\\SuperStarServer\\src\\file\\"+SnowflakeIdGenerator.ID(12)+"."+name.split("\\.")[1];
                            StreamUtils.byteArrayToStream(homework.getAnswers().get(i).getPic().getContent(),path);
                        }
                        pstmt2.setString(5, name);
                        pstmt2.setString(6,path);
                        pstmt2.setString(7,homework.getAnswers().get(i).getQuestion());
                        pstmt2.executeUpdate();
                        FlushCourse.flush(homework.getStudentid());
                        FlushCourse.flush(homework.getTeacherid());
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("提交作业失败");
        }
    }
}
